package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.common.core.util.lambda.Lambdas;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.supervision.convert.JzdxAccompliceConvert;
import com.engine4cloud.tdf.supervision.convert.JzdxAccompliceCrimeNameConvert;
import com.engine4cloud.tdf.supervision.dto.create.JzdxAccompliceCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxAccompliceCrimeNameQueryDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxAccompliceQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxAccompliceCrimeNameResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxAccompliceResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxAccompliceUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxAccompliceCrimeNameEntity;
import com.engine4cloud.tdf.supervision.entity.JzdxAccompliceEntity;
import com.engine4cloud.tdf.supervision.mapper.JzdxAccompliceMapper;
import com.engine4cloud.tdf.supervision.service.JzdxAccompliceCrimeNameService;
import com.engine4cloud.tdf.supervision.service.JzdxAccompliceService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 矫正对象同案犯信息表
 *
 * @author wenjie
 * @date 2024-04-01 16:13:51
 */
@Service
public class JzdxAccompliceServiceImpl extends ServiceImpl<JzdxAccompliceMapper, JzdxAccompliceEntity> implements JzdxAccompliceService {
    @Resource
    private JzdxService jzdxService;
    @Resource
    private JzdxAccompliceCrimeNameService accompliceCrimeNameService;

    @Override
    public PageInfo<JzdxAccompliceResultDTO> pageList(Params pageable, JzdxAccompliceQueryDTO dto) {
        IPage<JzdxAccompliceResultDTO> page = this.baseMapper.pageList(Pages.page(pageable), dto);
        //设置罪名
        Lambdas.each(page.getRecords(), this::setCrimeNameList);
        return Pages.convert(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(JzdxAccompliceCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        JzdxAccompliceEntity entity = JzdxAccompliceConvert.INSTANCE.create(dto, dx.getTenantId());
        //设置同案犯编号
        entity.setTafbh(CodeUtils.entrustCode());
        //保存主体
        super.save(entity);
        //保存罪名
        List<JzdxAccompliceCrimeNameEntity> crimeNameEntityList = JzdxAccompliceCrimeNameConvert.INSTANCE.convertList(entity, dto.getCrimeNameList());
        if (CollectionUtils.isNotEmpty(crimeNameEntityList)) {
            accompliceCrimeNameService.saveBatch(crimeNameEntityList);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(JzdxAccompliceUpdateDTO dto) {
        JzdxAccompliceEntity old = Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        super.updateById(JzdxAccompliceConvert.INSTANCE.update(dto));
        //更新罪名
        return accompliceCrimeNameService.updateBatch(old, dto.getCrimeNameList());
    }

    @Override
    public JzdxAccompliceResultDTO getById(Long id) {
        JzdxAccompliceEntity entity = Optional.ofNullable(super.getById(id))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        JzdxAccompliceResultDTO result = JzdxAccompliceConvert.INSTANCE.convert(entity);

        //设置罪名
        List<JzdxAccompliceCrimeNameResultDTO> crimeNameList = accompliceCrimeNameService.findList(new JzdxAccompliceCrimeNameQueryDTO(result.getTafbh()));
        result.setCrimeNameList(crimeNameList);
        return result;
    }

    private void setCrimeNameList(JzdxAccompliceResultDTO accompliceResult) {
        if (Objects.isNull(accompliceResult)) {
            return;
        }

        List<JzdxAccompliceCrimeNameResultDTO> crimeNameList = accompliceCrimeNameService.findList(new JzdxAccompliceCrimeNameQueryDTO(accompliceResult.getTafbh()));
        accompliceResult.setCrimeNameList(crimeNameList);
    }
}